package hessian.service;

import java.lang.reflect.Type;

import org.apache.lucene.document.Field.Store;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class EnumSerializer implements JsonSerializer<Store>, JsonDeserializer<Store> {

	// 对象转为Json时调用,实现JsonSerializer<PackageState>接口
	@Override
	public JsonElement serialize(Store state, Type arg1, JsonSerializationContext arg2) {
		return new JsonPrimitive(state.ordinal());
	}

	// json转为对象时调用,实现JsonDeserializer<PackageState>接口
	@Override
	public Store deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		if (json.getAsInt() < Store.values().length)
			return Store.values()[json.getAsInt()];
		return null;
	}

}
